package com.kuai.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LogKuaiTools {
	public  List<ChangeLog> arrayChange(Object old, Object lately){
		List<ChangeLog> logs = new ArrayList<ChangeLog>();
		if(!old.getClass().isInstance(lately)) {
			log.info("数据类型不一致");
		}
		IdentityHashMap<String, Object> silder = new IdentityHashMap<String, Object>();
		if(old instanceof ArrayList) {
			List oldBean = (List) old; 
			for (int i = 0; i < oldBean.size(); i++) {
				
				List<ChangeEntity> list = getChangeLog(oldBean.get(i));
				list.forEach(e->{
					silder.put(e.getField(), e.getValue());
				});
				List latelyBean = (List) lately; 
				for (int j = 0; j < latelyBean.size(); j++) {
					List<ChangeEntity> lists = getChangeLog(latelyBean.get(j));
					lists.forEach(e->{
						silder.put(e.getField(), e.getValue());
					});
				}
			}
		}
		return null;
	}
	/**
	 * @param objectOld 
	 * @desc 全部新增
	 * @return
	 */
	public List<ChangeLog> addAll(@SuppressWarnings("rawtypes") ArrayList objectOld){
		List<ChangeLog> logs = new ArrayList<ChangeLog>();
		for (int i = 0; i < objectOld.size(); i++) {
			String option = null;
			List<ChangeEntity> entitys = getChangeLog(objectOld.get(i));
			for (int j = 0; j < entitys.size(); j++) {
				ChangeEntity entity = entitys.get(j);
				ChangeLog el = new ChangeLog();
				if(entity.getField().equals("id") && entity.getValue().equals("0")) {
					option = "新增";
				}
				el.setFalg(option);
				el.setAfterValue(entity.getValue());
				el.setBeforeValue("");
				el.setField(entity.getField());
				logs.add(el);
			}
			log.info(":::1====>{}",logs);
		}
		return logs;
	}
	
	/**
	 * @desc 实体对象变更比较
	 * @param old
	 * @param lately
	 * @return
	 */
	public List<ChangeLog> objectChange(Object old, Object lately) {
		List<ChangeEntity> resultOld = getChangeLog(old);
		List<ChangeEntity> resultLately = getChangeLog(old);
		List<ChangeLog> result = new ArrayList<ChangeLog>();
		for (int i = 0; i < resultOld.size(); i++) {
			ChangeLog log = new ChangeLog();
			for (int j = 0; j < resultLately.size(); j++) {
				if(resultOld.get(i).getField().equals(resultLately.get(j).getField())) {
					log.setField(resultOld.get(i).getField());
					log.setBeforeValue(resultOld.get(i).getValue());
					log.setAfterValue(resultLately.get(j).getValue());
				}
			}
			result.add(log);
		}
		return result;
	}
	
	public List<ChangeEntity> getChangeLog(Object obj){
		List<ChangeEntity> list = new ArrayList<ChangeEntity>();
		Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
			try {
				String value = field.get(obj).toString();
				ChangeEntity entity = new ChangeEntity();
				entity.setField(fieldName);
				entity.setValue(value);
				list.add(entity);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
        }
		return list;
	}
	@Data
	class ChangeLog{
		private String field;
		private String beforeValue;
		private String afterValue;
		private String falg;
	}
	@Data
	class ChangeEntity{
		private String field;
		private String value;
	}

}
